package cn.diancan.action.manager;


import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;

import cn.diancan.action.BaseAction;
import cn.diancan.exception.OutMsgException;
import cn.diancan.manager.security.ManagerAuth;
import cn.diancan.model.GoodsCategory;
import cn.diancan.vo.manager.GoodsCategoryManagerActionVo;
import cn.diancan.vo.other.Pager;

public class GoodsCategoryAction extends BaseAction implements ModelDriven<GoodsCategoryManagerActionVo> {


    private GoodsCategoryManagerActionVo requestVo = new GoodsCategoryManagerActionVo();

    private Pager pager = new Pager();

    //List
    @ManagerAuth
    public String goodsCategoryListPage() {
        try {
            pager.setPageNumber(requestVo.getPageNumber());
            pager.setPageSize(10);
            goodsCategoryService.findVoByPager(pager, requestVo.getGoodsCategoryParentId(), requestVo.getCategoryName());
            this.put("pager", pager);
            this.put("requestVo", requestVo);
            return freemarkView("/views/ftl/manager/goodscategory/goods_category_list.ftl");
        } catch (OutMsgException e) {
            return managerError(e.getMessage());
        }
    }

    @ManagerAuth
    public String createGoodsCategoryPage() {
        return freemarkView("/views/ftl/manager/goodscategory/create_goods_category.ftl");
    }

    @ManagerAuth
    public String createGoodsCategory() {
        try {
            GoodsCategory goodsCategory = goodsCategoryService.saveGoodsCategory(requestVo.getGoodsCategoryParentId(), requestVo.getCategoryName(),requestVo.getIsCanDiscount() );
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("goodsCategoryId", goodsCategory.getId());
            jsonObject.put("categoryName", goodsCategory.getCategoryName());
            return jsonFormat(jsonObject, "添加成功", "添加失败");
        } catch (OutMsgException e) {
            return jsonError(e);
        } catch (Exception e) {
            return jsonError("系统繁忙");
        }
    }

    @ManagerAuth
    public String goodsCategoryInfo() {
        try {
            GoodsCategory goodsCategory = goodsCategoryService.findGoodsCategoryById(requestVo.getGoodsCategoryId());
            this.put("goodsCategory", goodsCategory);
            return freemarkView("/views/ftl/manager/goodscategory/goods_category_info.ftl");
        } catch (OutMsgException e) {
            return managerError(e.getMessage());
        }
    }


    @ManagerAuth
    public String modifyGoodsCategory() {
        try {
            Boolean result = goodsCategoryService.updateGoodsCategory(requestVo.getGoodsCategoryId(), requestVo.getGoodsCategoryParentId(), requestVo.getCategoryName(),requestVo.getIsCanDiscount() );
            return jsonFormat(result, "修改成功", "修改失败");
        }   catch (Exception e) {
            return jsonError(e);
        }
    }

    @ManagerAuth
    public String removeGoodsCategory() {
        try {
            Boolean result = goodsCategoryService.deleteGoodsCategoryById(requestVo.getGoodsCategoryId());
            return jsonFormat(result, "删除成功", "删除失败");
        }   catch (Exception e) {
            return jsonError(e);
        }
    }

    @ManagerAuth
    public String goodsCategoryList() {
        List<GoodsCategory> list = goodsCategoryService.findGoodsCategoryList(requestVo.getGoodsCategoryParentId(), requestVo.getPageNumber(), requestVo.getPageSize());
        return jsonFormat(list);
    }


    @Override
    public GoodsCategoryManagerActionVo getModel() {
        return requestVo;
    }

}
